Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Math
Imports C4F.DevKit.PictureAcquisition
Namespace PictureAcquisitionApplication
	Public Partial Class DeviceItemsDetailsInThumbnails
	Inherits Form
		#Region "Globals"
		Private pictureAquisitionManager As PictureAcquisitionManager = Nothing
		Private deviceIdNew As String = String.Empty
		Private deviceItems As List(Of Image) = New List(Of Image)()
		Private lstPictureBox As List(Of PictureBox) = New List(Of PictureBox)()
		Private lstLabel As List(Of Label) = New List(Of Label)()
		Private lstCheckBox As List(Of CheckBox) = New List(Of CheckBox)()
#End Region

		#Region "Helper Method"
		Public Sub New()
			InitializeComponent()
		End Sub
		Public Sub New(ByVal manager As PictureAcquisitionManager, ByVal deviceId As String)
			deviceIdNew = deviceId
			pictureAquisitionManager = manager
			InitializeComponent()
			GetDeviceItemsDetailsInThumbnails(deviceIdNew)
		End Sub
		Private Sub GetDeviceItemsDetailsInThumbnails(ByVal deviceIdNew As String)
			Try
                Windows.Forms.Cursor.Current = Cursors.WaitCursor
				deviceItems = pictureAquisitionManager.GetThumbnails(deviceIdNew)
				RenderingThumbNails()
                Windows.Forms.Cursor.Current = Cursors.[Default]
			Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub RenderingThumbNails()
			Dim i As Integer = 0
			For Each dev As Image In deviceItems
				lstPictureBox.Add(New PictureBox())
				lstLabel.Add(New Label())
                lstCheckBox.Add(New CheckBox())
                If i Mod 2 <> 0 Then
                    DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).BeginInit()
                    lstPictureBox(i).Location = New System.Drawing.Point(215, CType((10 + Math.Floor(i / 2) * 130), Integer))
                    lstLabel(i).Location = New System.Drawing.Point(215, CType((115 + Math.Floor(i / 2) * 130), Integer))
                    lstLabel(i).Size = New System.Drawing.Size(205, 13)
                    lstLabel(i).Text = dev.Tag.ToString()
                    lstCheckBox(i).Location = New System.Drawing.Point(345, CType((58 + Math.Floor(i / 2) * 130), Integer))
                    lstCheckBox(i).Text = String.Empty
                    'To contract the size of the checkbox so that it should not overlap to the adjacent picturebox.
                    lstCheckBox(i).Size = New Size(20, 20)
                    'lstPictureBox[i].Name = "pictureBox1";
                    lstPictureBox(i).Size = New System.Drawing.Size(100, 100)
                    lstPictureBox(i).Image = dev
                    Me.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox(i))
                    Me.pnlDeviceItemInThumbNails.Controls.Add(lstLabel(i))
                    Me.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox(i))
                    DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).EndInit()
                End If
				If i Mod 2 = 0 Then
					DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).BeginInit()
                    lstPictureBox(i).Location = New System.Drawing.Point(10, CType((10 + Math.Floor(i / 2) * 130), Integer))
                    lstLabel(i).Location = New System.Drawing.Point(10, CType((115 + Math.Floor(i / 2) * 130), Integer))
					lstLabel(i).Size = New System.Drawing.Size(205, 13)
					lstLabel(i).Text = dev.Tag.ToString()
                    lstCheckBox(i).Location = New System.Drawing.Point(140, CType((58 + Math.Floor(i / 2) * 130), Integer))
					lstCheckBox(i).Text = String.Empty
					'To contract the size of the checkbox so that it should not overlap to the adjacent picturebox.
					lstCheckBox(i).Size = New Size(20, 20)
					'lstPictureBox[i].Name = "pictureBox1";
					lstPictureBox(i).Size = New System.Drawing.Size(100, 100)
					lstPictureBox(i).Image = dev
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstLabel(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox(i))
					DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).EndInit()
				End If
				i += 1
			Next
		End Sub
		Private Sub RenderingThumbNailsFromPictureBox()
			Dim i As Integer = 0
			While i < lstPictureBox.Count
				If i Mod 2 <> 0 Then
                    lstPictureBox(i).Location = New System.Drawing.Point(215, CType((10 + Math.Floor(i / 2) * 130), Integer))
                    lstLabel(i).Location = New System.Drawing.Point(215, CType((115 + Math.Floor(i / 2) * 130), Integer))
                    lstCheckBox(i).Location = New System.Drawing.Point(345, CType((58 + Math.Floor(i / 2) * 130), Integer))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstLabel(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox(i))
					DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).EndInit()
				End If
				If i Mod 2 = 0 Then
					DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).BeginInit()
                    lstPictureBox(i).Location = New System.Drawing.Point(10, CType((10 + Math.Floor(i / 2) * 130), Integer))
                    lstLabel(i).Location = New System.Drawing.Point(10, CType((115 + Math.Floor(i / 2) * 130), Integer))
                    lstCheckBox(i).Location = New System.Drawing.Point(140, CType((58 + Math.Floor(i / 2) * 130), Integer))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstPictureBox(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstLabel(i))
					Me.pnlDeviceItemInThumbNails.Controls.Add(lstCheckBox(i))
					DirectCast((lstPictureBox(i)), System.ComponentModel.ISupportInitialize).EndInit()
				End If
				i += 1
			End While
		End Sub
		Private Sub DeleteDeviceItemInThumbnails(ByVal deviceIdNew As String)
			Try
				Dim i As Integer = 0
				Dim SelectedCheckBoxIndex As Integer = 0
				Dim lstOfSelectedCheckBox As List(Of Int32) = New List(Of Int32)()
				Dim lstOfNotSelectedCheckBox As List(Of Int32) = New List(Of Int32)()
				For i = 0 To lstCheckBox.Count - 1
					If lstCheckBox(i).Checked Then
						lstOfSelectedCheckBox.Add(i)
					Else
						lstOfNotSelectedCheckBox.Add(i)
					End If
				Next
                Windows.Forms.Cursor.Current = Cursors.WaitCursor
				For SelectedCheckBoxIndex = 0 To lstOfSelectedCheckBox.Count - 1
					Dim itemName As String = lstLabel(lstOfSelectedCheckBox(SelectedCheckBoxIndex)).Text
					pictureAquisitionManager.DeleteItem(itemName, deviceIdNew)
				Next
				DeletePictureDataFromListBoxex(lstOfNotSelectedCheckBox)
				RenderingThumbNailsFromPictureBox()
                Windows.Forms.Cursor.Current = Cursors.[Default]
			Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
		Private Sub DeletePictureDataFromListBoxex(ByVal remainingPhotos As List(Of Int32))
			Dim templstPictureBox As List(Of PictureBox) = New List(Of PictureBox)()
			Dim templstLabel As List(Of Label) = New List(Of Label)()
			Dim templstCheckBox As List(Of CheckBox) = New List(Of CheckBox)()
			For Each remainingIndex As Integer In remainingPhotos
				templstPictureBox.Add(lstPictureBox(remainingIndex))
				templstLabel.Add(lstLabel(remainingIndex))
				templstCheckBox.Add(lstCheckBox(remainingIndex))
			Next
			Dim totalItem As Integer = lstPictureBox.Count
			For i As Integer = 0 To totalItem - 1
				pnlDeviceItemInThumbNails.Controls.Remove(lstPictureBox(i))
				pnlDeviceItemInThumbNails.Controls.Remove(lstLabel(i))
				pnlDeviceItemInThumbNails.Controls.Remove(lstCheckBox(i))
			Next
			lstPictureBox = templstPictureBox
			lstLabel = templstLabel
			lstCheckBox = templstCheckBox
		End Sub
		Private Sub AcquireSelectedDeviceItemsInThumbnails(ByVal deviceIdNew As String)
			Try
				Dim i As Integer = 0
				Dim SelectedCheckBoxIndex As Integer = 0
				Dim lstOfSelectedCheckBox As List(Of Int32) = New List(Of Int32)()
				Dim lstOfSelectedItemNames As List(Of String) = New List(Of String)()
				For i = 0 To lstCheckBox.Count - 1
					If lstCheckBox(i).Checked Then
						lstOfSelectedCheckBox.Add(i)
					End If
				Next
				For SelectedCheckBoxIndex = 0 To lstOfSelectedCheckBox.Count - 1
					lstOfSelectedItemNames.Add(lstLabel(lstOfSelectedCheckBox(SelectedCheckBoxIndex)).Text)
				Next
				Dim fileBrowserDialog As FolderBrowserDialog = New FolderBrowserDialog()
				fileBrowserDialog.Description = "Choose folder to import selected files"
				fileBrowserDialog.ShowNewFolderButton = True
                If Windows.Forms.DialogResult.OK = fileBrowserDialog.ShowDialog() Then
                    Windows.Forms.Cursor.Current = Cursors.WaitCursor
                    pictureAquisitionManager.AcquireItems(deviceIdNew, lstOfSelectedItemNames, fileBrowserDialog.SelectedPath + "\")
                    Windows.Forms.Cursor.Current = Cursors.[Default]
                End If
			Catch ex As ArgumentNullException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As ArgumentException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			Catch ex As PictureAcquisitionException
                Windows.Forms.Cursor.Current = Cursors.[Default]
				MessageBox.Show(ex.Message, "Picture Acquisition", MessageBoxButtons.OK, MessageBoxIcon.[Error])
			End Try
		End Sub
#End Region

		#Region "Events Handler"
        Private Sub btnDeleteDeviceItemsInThumbnails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDeleteDeviceItemsInThumbnails.Click
            DeleteDeviceItemInThumbnails(deviceIdNew)
        End Sub
        Private Sub btnDeviceItemDetailsInThumbnailsImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDeviceItemDetailsInThumbnailsImport.Click
            AcquireSelectedDeviceItemsInThumbnails(deviceIdNew)
        End Sub
        Private Sub btnCloseDeviceItemsInThumbnails_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCloseDeviceItemsInThumbnails.Click
            Me.Close()
        End Sub
        Private Sub DeviceItemsDetailsInThumbnails_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
            If e.KeyCode = Keys.Escape Then
                Me.Close()
            End If
        End Sub
		#End Region

    End Class
End Namespace
